package com.ygsoft.tianlu_mall.service.impl;

import com.ygsoft.tianlu_mall.mapper.CartMapper;
import com.ygsoft.tianlu_mall.mapper.OrderMapper;
import com.ygsoft.tianlu_mall.pojo.vo.req.AddEvaluationReqVO;
import com.ygsoft.tianlu_mall.service.OrderService;
import com.ygsoft.tianlu_mall.util.OrderCodeGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    CartMapper cartMapper;

    @Override
    @Transactional
    public void addOneGoodsToOrder(int user_id, int goods_id) {
        HashMap<String,Object> orderMap = new HashMap<>();
        HashMap<String,Object> order2detailMap = new HashMap<>();
        //订单创建时间
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("yyyy-MM-DD HH:MM:SS");
        Date create_time = new Date();
        //订单号
        String order_code = OrderCodeGenerator.getOrderCode();
        //订单总价 = 商品价格
        double goods_price = orderMapper.getGoodsPrice(goods_id);
        BigDecimal order_totalMoney = BigDecimal.valueOf(goods_price);
        orderMap.put("create_time",create_time);
        orderMap.put("order_code",order_code);
        orderMap.put("order_totalMoney",order_totalMoney);
        orderMap.put("user_id",user_id);
        orderMapper.addGoodsToOrder(orderMap);

        //获得订单id
        int order_id = orderMapper.getOrderId();
        //购得的商品与店铺关联，方便店铺查订单
        int shop_id = cartMapper.getShopIdByGoodsId(goods_id);
        order2detailMap.put("order_id",order_id);
        order2detailMap.put("goods_id",goods_id);
        //0 待支付 1 已支付 2 待评价 3 完成
        order2detailMap.put("order_status",0);
        order2detailMap.put("shop_id",shop_id);
        orderMapper.addGoodsToOrder2Detail(order2detailMap);
    }


    @Override
    @Transactional
    public void addGoodsToOrderByShopId(int user_id, int shop_id, List<Integer> goods_idList, double total_money) {
        HashMap<String,Object> orderMap = new HashMap<>();
        HashMap<String,Object> order2detailMap = new HashMap<>();
        //订单创建时间
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("yyyy-MM-DD HH:MM:SS");
        Date create_time = new Date();
        //订单号
        String order_code = OrderCodeGenerator.getOrderCode();
        BigDecimal order_totalMoney = BigDecimal.valueOf(total_money);
        orderMap.put("create_time",create_time);
        orderMap.put("order_code",order_code);
        orderMap.put("order_totalMoney",order_totalMoney);
        orderMap.put("user_id",user_id);
        orderMapper.addGoodsToOrder(orderMap);

        //获得订单id
        int order_id = orderMapper.getOrderId();
        for (int goods_id:goods_idList) {
            order2detailMap.put("order_id",order_id);
            order2detailMap.put("goods_id",goods_id);
            //0 待支付 1 已支付 2 待评价 3 完成
            order2detailMap.put("order_status",0);
            order2detailMap.put("shop_id",shop_id);
            orderMapper.addGoodsToOrder2Detail(order2detailMap);
        }
    }

    @Override
    public int getOrderId() {
        return orderMapper.getOrderId();
    }

    @Override
    public HashMap<String, Object> getOrderGoodsInfo(int user_id, int order_id) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("user_id",user_id);
        map.put("order_id",order_id);
        //买家信息
        HashMap<String,Object> userInfo = orderMapper.getUserInfo(user_id);
        //按订单id获得信息 店名，商品id，商品封面，商品名称，商品类型，商品价格
        List<HashMap<String,Object>> orderGoodsInfo = orderMapper.orderGoodsInfo(map);
        HashMap<String,Object> result = new HashMap<>();
        result.put("orderGoodsInfo",orderGoodsInfo);
        result.put("userInfo",userInfo);
        return result;
    }

    @Override
    public BigDecimal getUserRestMoney(int user_id) {
        return orderMapper.getUserRestMoney(user_id);
    }

    @Override
    public void updateUserMoney(int user_id, BigDecimal total_money) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("user_id",user_id);
        map.put("total_money",total_money);
        orderMapper.updateUserMoney(map);
    }

    @Override
    public List<HashMap<String, Object>> getGoodsIdByOrderId(int order_id) {
        return orderMapper.getGoodsIdByOrderId(order_id);
    }

    @Override
    public void updateOrder2DetailStatus(HashMap<String,Object> map) {
        orderMapper.updateOrder2DetailStatus(map);
    }

    @Override
    public int getShopUserid(int order_id) {
        return orderMapper.getShopUserid(order_id);
    }

    @Override
    public BigDecimal getEveryOrderMoney(int order_id) {
        return orderMapper.getEveryOrderMoney(order_id);
    }

    @Override
    public void updateShopUserMoney(int shop_userid,BigDecimal money) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("user_id",shop_userid);
        map.put("total_money",money);
        orderMapper.updateShopUserMoney(map);
    }

    @Override
    @Transactional
    public HashMap<String, Object> getGoodsFile(int user_id, List<Integer> order_idList) {
        HashMap<String,Object> goodsFileMap = new HashMap<>();
        for (Integer order_id:order_idList){
            HashMap<String,Object> map = new HashMap<>();
            map.put("user_id",user_id);
            map.put("order_id",order_id);
            List<HashMap<String,Object>> goodsFileInfo = orderMapper.getGoodsFile(map);
            goodsFileMap.put("order_id"+order_id.toString(),goodsFileInfo);
        }
        return goodsFileMap;
    }

    @Override
    @Transactional
    public List<HashMap<String,Object>> getAllOrderInfo(int user_id) {
        List<HashMap<String,Object>> orderInfo = orderMapper.getOrderInfo(user_id);
        return orderInfo;
    }

    @Override
    public void updateOrderEndTime(HashMap<String, Object> closeMap) {
        updateOrderEndTime(closeMap);
    }

    @Override
    @Transactional
    public void deleteOrderInfo(int user_id, int order_id) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("user_id",user_id);
        map.put("order_id",order_id);
        orderMapper.deleteOrder(map);
        orderMapper.deleteOrder2detail(order_id);
    }

    @Override
    @Transactional
    public void addEvaluation(AddEvaluationReqVO vo) {
        HashMap<String,Object> map = new HashMap<>();
        HashMap<String,Object> statusMap = new HashMap<>();
        HashMap<String,Object> evaluationMap = new HashMap<>();
        HashMap<String,Object> orderMap = new HashMap<>();
        //评论时间
        Date time = new Date();
        //根据商品id获得订单id
        HashMap<String,Object> getOrderIdMap = new HashMap<>();
        getOrderIdMap.put("goods_id",vo.getGoods_id());
        getOrderIdMap.put("user_id",vo.getUser_id());
        int order_id = orderMapper.getOrderIdByGoodsid(getOrderIdMap);
        //根据商品id获得店铺id
        int shop_id = cartMapper.getShopIdByGoodsId(vo.getGoods_id());
        map.put("content",vo.getContent());
        map.put("time",time);
        map.put("service",vo.getService());
        map.put("description",vo.getDescription());
        map.put("user_id",vo.getUser_id());
        map.put("order_id",order_id);
        map.put("goods_id",vo.getGoods_id());
        map.put("shop_id",shop_id);
        orderMapper.addEvaluation(map);
        //更改订单状态 1 待评价->2 完成
        statusMap.put("order_id",order_id);
        statusMap.put("order_status",2);
        statusMap.put("goods_id",vo.getGoods_id());
        orderMapper.updateOrder2DetailStatus2(statusMap);
        //在订单详细表绑定评价id
        int evaluation_id = orderMapper.getEvaluation();
        evaluationMap.put("order_id",order_id);
        evaluationMap.put("evaluation_id",evaluation_id);
        evaluationMap.put("goods_id",vo.getGoods_id());
        orderMapper.updateOrder2DetailEvaluationId(evaluationMap);
        //订单结束
        List<HashMap<String,Object>> statusResult = orderMapper.getGoodsStatus(order_id);
        Boolean end = true;
        for (HashMap<String, Object> statusCode : statusResult) {
            if(statusCode.get("order_status").equals(1)){
                end = false;
            }
        }
        if (end){
            orderMap.put("order_id",order_id);
            orderMap.put("end_time",new Date());
            orderMapper.updateOrderEndTime(orderMap);
        }

    }
}
